###################################################################################################
# Term Limits and Legislative Polarization ########################################################
# Michael Olson and Jon Rogowski ##################################################################
###################################################################################################

# This script generates results presented in Table B.8 in the supplementary materials.

###################################################################################################
# working directory and packages ##################################################################
###################################################################################################

  #setwd("./termlimits_polarization_analysisdata")

###################################################################################################
# state legislative election-merged data ##########################################################
###################################################################################################

  sle_sm_dat <- read.csv("sle_sm_dat.csv")

# make party variable

  sle_sm_dat$party <- str_sub(sle_sm_dat$caucus_year,-1)
  
# make chamber variable
  
  sle_sm_dat$chamber <- str_sub(sle_sm_dat$caucus_year,9,-3)
  
# make alternative outcome (for presentation purposes)
  
  sle_sm_dat$np_score_dem <- sle_sm_dat$np_score
  
# make chamber-specific abs_diff
  
  sle_sm_dat$abs_diff <- sle_sm_dat$hs_abs_diff
  sle_sm_dat$abs_diff[sle_sm_dat$chamber=="senate"] <- sle_sm_dat$sen_abs_diff[sle_sm_dat$chamber=="senate"]
  
# create marginal variable
  
  sle_sm_dat$marginal <- NA
  sle_sm_dat[sle_sm_dat$pct<=0.6,"marginal"] <- 1
  sle_sm_dat[sle_sm_dat$pct>0.6,"marginal"] <- 0
  
# make professionalims logged
  
  sle_sm_dat$legprofscore <- log(sle_sm_dat$legprofscore)

###################################################################################################
# Analysis ########################################################################################
###################################################################################################

  fe_margin_allcov_rep <- felm(np_score~term_limit*marginal
                               +divided_gov
                               +legprofscore+abs_diff
                               |state+year|0|state
                               ,data=sle_sm_dat[sle_sm_dat$party=="R",])
  
  fe_margin_allcov_dem <- felm(np_score_dem~term_limit*marginal
                               +divided_gov
                               +legprofscore+abs_diff
                               |state+year|0|state
                               ,data=sle_sm_dat[sle_sm_dat$party=="D",])
  
  fe_margin_allcov_senate_rep <- felm(np_score~term_limit*marginal
                               +divided_gov
                               +legprofscore+abs_diff
                               |state+year|0|state
                               ,data=sle_sm_dat[sle_sm_dat$party=="R" & sle_sm_dat$chamber=="senate",])
  
  fe_margin_allcov_senate_dem <- felm(np_score_dem~term_limit*marginal
                               +divided_gov
                               +legprofscore+abs_diff
                               |state+year|0|state
                               ,data=sle_sm_dat[sle_sm_dat$party=="D" & sle_sm_dat$chamber=="senate",])
  
  fe_margin_allcov_house_rep <- felm(np_score~term_limit*marginal
                               +divided_gov
                               +legprofscore+abs_diff
                               |state+year|0|state
                               ,data=sle_sm_dat[sle_sm_dat$party=="R" & sle_sm_dat$chamber=="house",])
  
  fe_margin_allcov_house_dem <- felm(np_score_dem~term_limit*marginal
                               +divided_gov
                               +legprofscore+abs_diff
                               |state+year|0|state
                               ,data=sle_sm_dat[sle_sm_dat$party=="D" & sle_sm_dat$chamber=="house",])

 margin_sg <- stargazer(fe_margin_allcov_dem,fe_margin_allcov_house_dem,fe_margin_allcov_senate_dem,
                        fe_margin_allcov_rep,fe_margin_allcov_house_rep,fe_margin_allcov_senate_rep,
                                keep=c("term_limit","margin",
                                       "divided_gov","gov_party","legprofscore","abs_diff"),
                                add.lines = list(c("Chamber","Pooled","House","Senate","Pooled","House","Senate"),
                                  c("State Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                                 c("Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                                 c("Projected $R^2$",round(summary(fe_margin_allcov_dem)$P.r.squared,3),
                                                   round(summary(fe_margin_allcov_house_dem)$P.r.squared,3),round(summary(fe_margin_allcov_senate_dem)$P.r.squared,3),
                                                   round(summary(fe_margin_allcov_rep)$P.r.squared,3),round(summary(fe_margin_allcov_house_rep)$P.r.squared,3),
                                                   round(summary(fe_margin_allcov_senate_rep)$P.r.squared,3))),
                                notes.append = FALSE,notes.label = "",
                                notes="\\parbox[t]{0.975\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                                standard errors clustered on states in parentheses. $^{**}$p$<$0.05, $^*$p$<$0.10 (two-tailed test).}",  
                                omit.stat = c("rsq", "f", "ser","adj.rsq"),
                                dep.var.labels = c("Democrats","Republicans"),dep.var.caption = "Shor-McCarty Ideal Point",
                         star.char=c("*","**"),star.cutoffs = c(0.10,0.05),digits=3,digits.extra=0,
                                covariate.labels=c("Term Limits","Marginal District","Divided Gov.","ln(Leg. Professionalism)","Party Competitiveness",
                                                   "Term Limit $\\times$ Marginal District"),
                                label="individual_margins_results",table.placement = "!ht",
                                table.layout ="-ld-#-t-as-n",
                                title="Term Limits and Polarization: Heterogeneity by District Marginality")
  
  cat(margin_sg, sep = '\n', file = paste(output_path,"margin.tex",sep=""))
  
# reset working directory
  
  setwd(storewd)
  